package com.frank.fresh_mall.controller;

import com.frank.fresh_mall.common.ApiRestResponse;
import com.frank.fresh_mall.common.Constant;
import com.frank.fresh_mall.exception.FreshMallException;
import com.frank.fresh_mall.exception.FreshMallExceptionEnum;
import com.frank.fresh_mall.model.pojo.User;
import com.frank.fresh_mall.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {
    @Resource
    private UserService userService;

    @GetMapping("/user/{id}")
    @ResponseBody
    public String getUser(@PathVariable("id") Integer userId) {
        User user = userService.selectUserById(userId);
        return user.toString();
    }

    @PostMapping("/register")
    @ResponseBody
    public ApiRestResponse register(@RequestParam("userName") String userName,
                                    @RequestParam("password") String password) throws FreshMallException {
        if (StringUtils.isEmpty(userName)) {// 用户名为空
            return ApiRestResponse.error(FreshMallExceptionEnum.NEED_USER_NAME);
        }
        if (StringUtils.isEmpty(password)) {
            return ApiRestResponse.error(FreshMallExceptionEnum.NEED_PASSWORD);
        }
        // 密码长度不能少于8位
        if (password.length() < 8) {
            return ApiRestResponse.error(FreshMallExceptionEnum.PASSWORD_TOO_SHORT);
        }

        userService.register(userName, password);
        return ApiRestResponse.success();
    }

    @PostMapping("/login")
    @ResponseBody
    public ApiRestResponse login(@RequestParam("userName") String userName,
                                 @RequestParam("password") String password,
                                 HttpSession session) throws FreshMallException {
        if (StringUtils.isEmpty(userName)) {// 用户名为空
            return ApiRestResponse.error(FreshMallExceptionEnum.NEED_USER_NAME);
        }
        if (StringUtils.isEmpty(password)) {
            return ApiRestResponse.error(FreshMallExceptionEnum.NEED_PASSWORD);
        }
        // 密码长度不能少于8位
        if (password.length() < 8) {
            return ApiRestResponse.error(FreshMallExceptionEnum.PASSWORD_TOO_SHORT);
        }
        User user = userService.login(userName, password);
        // 保存用户信息时，不保存密码
        user.setPassword(null);
        // 将用户信息存放到session中
        session.setAttribute(Constant.FRESH_MALL_USER, user);
        return ApiRestResponse.success(user);
    }

    /**
     * 更新用户签名
     *
     * @param session   session对象
     * @param signature 签名信息
     * @return 返回通用响应
     * @throws FreshMallException
     */
    @PostMapping("/user/update")
    @ResponseBody
    public ApiRestResponse updateUserInfo(HttpSession session,
                                          @RequestParam("signature") String signature) throws FreshMallException {
        User currentUser = (User) session.getAttribute(Constant.FRESH_MALL_USER);
        if (currentUser == null) {
            return ApiRestResponse.error(FreshMallExceptionEnum.NEED_LOGIN);
        }
        // 找到用户
        User user = new User();
        user.setId(currentUser.getId());
        user.setPersonalizedSignature(signature);
        userService.updateInformation(user);
        return ApiRestResponse.success();
    }

    // 登出，清除session
    @PostMapping("/user/logout")
    @ResponseBody
    public ApiRestResponse logout(HttpSession session) {
        session.removeAttribute(Constant.FRESH_MALL_USER);
        return ApiRestResponse.success();
    }

    // 后台管理员登录
    @PostMapping("/adminLogin")
    @ResponseBody
    public ApiRestResponse adminLogin(@RequestParam("userName") String userName,
                                      @RequestParam("password") String password,
                                      HttpSession session) throws FreshMallException {
        if (StringUtils.isEmpty(userName)) {// 用户名为空
            return ApiRestResponse.error(FreshMallExceptionEnum.NEED_USER_NAME);
        }
        if (StringUtils.isEmpty(password)) {
            return ApiRestResponse.error(FreshMallExceptionEnum.NEED_PASSWORD);
        }
        // 密码长度不能少于8位
        if (password.length() < 8) {
            return ApiRestResponse.error(FreshMallExceptionEnum.PASSWORD_TOO_SHORT);
        }

        User user = userService.login(userName, password);
        // 校验是否是管理员
        if (userService.checkAdminRole(user)) {
            // 是管理员，执行操作
            // 保存用户信息时，不保存密码
            user.setPassword(null);
            // 将用户信息存放到session中
            session.setAttribute(Constant.FRESH_MALL_USER, user);
            return ApiRestResponse.success(user);
        }
        return ApiRestResponse.error(FreshMallExceptionEnum.NEED_ADMIN);
    }
}
